'''
Created on 2011-4-1

@author: Chine
'''
import urllib2, urllib

from weibo import callable
from ChineKingBlog.weibopy.utils import import_simplejson

class Record(object):
    def __init__(self, adict):
        assert adict, dict
        setattr(self, 'title', adict['title'])
        setattr(self, 'htmlSnippet', adict['htmlSnippet'])
        setattr(self, 'link', adict['link'])

class SearchModel(callable):
    def __init__(self, q):
        self._q = q
        
class GoogleSearch(SearchModel):
    key = 'AIzaSyD2CyYgsRzvz_xjW7SUlgwxQ_PFy4Y2UJM'
    cx = '012746924678297800857:aiwcuwu6kvg'
    
    url = 'https://www.googleapis.com/customsearch/v1'
    
    def __init__(self, q, page=1):
        SearchModel.__init__(self, q)
        self._page = page
    
    def _get_data(self):
        start = (self._page - 1) * 10 + 1 
        
        data = {'q': self._q.encode("utf-8")}
        str = urllib.urlencode(data)
        
        abs_url = "https://www.googleapis.com/customsearch/v1?key=%s&cx=%s&%s&start=%d" % \
        (self.key, self.cx, str, start)
        
        data = urllib2.urlopen(abs_url)
        
        resultContent = data.read()
        #print resultContent
        return resultContent
    
    def _get_json(self):
        if getattr(self, '_json', None) is None:
            json = import_simplejson()
            self._json = json.loads(self._get_data())
        return self._json

    def _get_count(self):
        _json = self._get_json()
        return int(_json['queries']['request'][0]['totalResults'])
    
    def _get_result_list(self):
        _json = self._get_json()
        return _json['items']
    
    def __call__(self):
        records = []
        try:
            results =  self._get_result_list()
            for r in results:
                record = Record(r)
                records.append(record)
        except KeyError:
            pass
            # print KeyError.message
        return self._get_count(), records
        
class ExampleSearch(GoogleSearch):
    cx = '017576662512468239146:omuauf_lfve'
    
if __name__ == "__main__":
    s = ExampleSearch("lecture")
    print s()[1]